package com.wushi.lenovo.asia5b.app_ui.h5;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.h5.H5Bean;
import com.wushi.lenovo.asia5b.app_model.h5.H5TitleBean;
import com.wushi.lenovo.asia5b.app_model.other.CategoryModel;
import com.wushi.lenovo.asia5b.app_model.other.WebUrlType;
import com.wushi.lenovo.asia5b.app_utils.helper.H5Hepler;

import butterknife.BindView;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2019/3/4.
 * 品牌分类/商品分类/搜索
 * 这个类和HelperActivity一模一样,只是为了区分功能而分开两个类
 */
public class CategoryActivity extends H5Activity {
    @BindView(R.id.pb_category)
    ContentLoadingProgressBar progress;
    @BindView(R.id.ll_category_vessel)
    LinearLayout ll_vessel;
    @BindView(R.id.tv_title_titletext)
    TextView tv_title_titletext;
    @BindView(R.id.rl_title)
    RelativeLayout rl_title;
    protected String url;
    protected WebView webView;

    public static void startCategoryActivity(Context context, WebUrlType webUrlType) {
        String url = "";
        switch (webUrlType) {
            case aboutus:
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "mobile/about-us/index";
                break;
            case helpcenter:
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "mobile/help-center/index";
                break;
            case privacypolicy:
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "mobile/privacy-policy/index";
                break;
            case termsofuse:
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "mobile/terms-of-use/index";
                break;
            case shopcar_daigou:
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "terms-of-order";
                break;
            case shopcar_pi:
//                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "privacy-policy";
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "terms-of-order";
                break;
            case termsconditions_creditcard:
                url = GlobalValue.BASE_PAYMENT_THIRD_URL + "mobile/credit-card-terms-and-conditions/index";
                break;
            case termsconditions_fpx:
                url = "https://www.mepsfpx.com.my/FPXMain/termsAndConditions.jsp";
                break;
        }
        if (!TextUtils.equals(LocalManageUtil.getAppLanguage(), "en")) {
            if (webUrlType == WebUrlType.aboutus||webUrlType == WebUrlType.helpcenter) {
                url += "/cn";
            }
        }
        String title = context.getString(webUrlType.getTitle());
        startCategoryActivity(context, url, title);
    }

    public static void startCategoryActivity(Context context, CategoryModel model) {
        String url = "";
        String title = "";
        if (model.getId() > 0) {
            url = H5Hepler.getH5_Url(String.format("cat_2?cat_id_1=%d", model.getId()));
            title = model.getCat_name();
        } else {
            url = H5Hepler.getH5_Url("cat?");
        }
        startCategoryActivity(context, url, title);
    }

    public static void startCategoryActivity(Context context, String url, String title) {
        url = H5Hepler.addAsiaH5Params(url);
        Intent intent = new Intent(context, CategoryActivity.class);
        intent.putExtra(H5_URL, url);
        if (!TextUtils.isEmpty(title)) {
            intent.putExtra(H5_TITLE, title);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_category;
    }

    @Override
    protected WebView getWebView() {
        return webView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getIntent().getStringExtra(H5_URL);
        String title = getIntent().getStringExtra(H5_TITLE);
        tv_title_titletext.setText("");
        if (!TextUtils.isEmpty(title)) {
            tv_title_titletext.setText(title);
        }
        initView();
        initEvent();
        initData();
    }

    @Override
    protected void initView() {
        //使用new方式这是为了解决当(settings.setJavaScriptEnabled(true))之后会出现TextToSpeech的内存泄漏
        webView = new WebView(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        webView.setLayoutParams(layoutParams);
        ll_vessel.addView(webView);
        setting(webView);
        setWebViewChormeClient(webView, progress);
    }

    @Override
    protected void callBackByJs(H5Bean h5Bean) {
        super.callBackByJs(h5Bean);
        if (h5Bean != null) {
            if (h5Bean instanceof H5TitleBean) {
                H5TitleBean titleBean = (H5TitleBean) h5Bean;
                if (tv_title_titletext != null) {
                    tv_title_titletext.setText(titleBean.getText());
                }
            }
        }
    }

    @Override
    protected void initData() {
        Timber.d("--->>>>>>>>---url:" + url);
        webView.loadUrl(url);
    }

    @Override
    protected void initEvent() {
        webView.addJavascriptInterface(jsCall, H5_JSCALL_NAME);
    }

}
